<?php

/**
 * This is the model class for table "{{wine}}".
 *
 * The followings are the available columns in table '{{wine}}':
 * @property integer $wine_id
 * @property string $vintage
 * @property string $grape_variety
 * @property string $location
 * @property string $quality
 * @property string $amount
 * @property string $country
 * @property string $area
 * @property string $section
 * @property integer $vintner_id
 * @property integer $supplier_id
 * @property integer $quantity
 * @property string $weight
 * @property double $bottle_size
 * @property string $alcohol_concentration
 * @property string $type
 * @property string $fastening
 * @property string $points
 * @property double $buying_price
 * @property double $sale_price_net
 * @property string $description_short
 * @property string $description_long
 *
 * The followings are the available model relations:
 * @property Purchase[] $purchases
 * @property Sale[] $sales
 * @property Vintner $vintner
 * @property Vintner $supplier
 */
class Wine extends ActiveRecord
{
    	//public $product_information;
        public $vorname;
        public $nachname;
	
        /**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Wine the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{wine}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
/* 			array('vintner_id, supplier_id', 'required'), */
			array('vintner_id, supplier_id, quantity', 'numerical', 'integerOnly'=>true),
			array('bottle_size, buying_price, sale_price_net', 'numerical'),
			array('vintage', 'length', 'max'=>4),
			array('grape_variety, location, quality, country, area, section', 'length', 'max'=>200),
            array('amount', 'length', 'max'=>20),
			array('weight', 'length', 'max'=>10),
			array('alcohol_concentration', 'length', 'max'=>5),
			array('type, fastening', 'length', 'max'=>50),
			array('points', 'length', 'max'=>100),
			array('description_short, description_long', 'safe'),
            array('vint', 'length', 'max'=>400),
            array('product_information', 'length', 'max'=>500),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('wine_id, vintage, grape_variety, location, quality, amount, country, area, section, vintner_id, supplier_id, quantity, weight, bottle_size, alcohol_concentration, type, fastening, points, buying_price, sale_price_net, description_short, description_long, vorname, nachname, vint, product_information', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'purchases' => array(self::HAS_MANY, 'Purchase', 'wine_id'),
			'sales' => array(self::HAS_MANY, 'Sale', 'wine_id'),
			'vintner' => array(self::BELONGS_TO, 'Vintner', 'vintner_id'),
			'supplier' => array(self::BELONGS_TO, 'Vintner', 'supplier_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'wine_id' => 'ID',
			'vintage' => 'Jahrgang',
			'grape_variety' => 'Traubensorte',
			'location' => 'Riede',
			'quality' => 'Qualität',
            'amount' => 'Istbest.',
            'country' => 'Land', 
            'area' => 'Gebiet', 
            'section' => 'Bereich',
			'vintner_id' => 'Weinbauer',
			'supplier_id' => 'Lieferant',
			'quantity' => 'Stück/Karton',
			'weight' => 'Gewicht',
			'bottle_size' => 'Flaschengröße',
			'alcohol_concentration' => 'Alkoholgehalt',
			'type' => 'Weinart',
			'fastening' => 'Verschlussart',
			'points' => 'Punktebewertung',
			'buying_price' => 'EKP Netto',
			'sale_price_net' => 'VKP Netto',
			'description_short' => 'Beschreibung (kurz)',
			'description_long' => 'Beschreibung (lang)',
            'vint' => 'Winzer',
            'product_information' => 'Bezeichnung',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

        $criteria->with = array( 'vintner' );
                
		$criteria->compare('wine_id',$this->wine_id);
		$criteria->compare('vintage',$this->vintage,true);
		$criteria->compare('grape_variety',$this->grape_variety,true);
		$criteria->compare('location',$this->location,true);
		$criteria->compare('quality',$this->quality,true);
        $criteria->compare('amount',$this->amount,true);
        $criteria->compare('country',$this->country,true);
		$criteria->compare('area',$this->area,true);
		$criteria->compare('section',$this->section,true);
		$criteria->compare('vintner_id',$this->vintner_id);
		$criteria->compare('supplier_id',$this->supplier_id);
		$criteria->compare('quantity',$this->quantity);
		$criteria->compare('weight',$this->weight,true);
		$criteria->compare('bottle_size',$this->bottle_size);
		$criteria->compare('alcohol_concentration',$this->alcohol_concentration,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('fastening',$this->fastening,true);
		$criteria->compare('points',$this->points,true);
		$criteria->compare('buying_price',$this->buying_price);
		$criteria->compare('sale_price_net',$this->sale_price_net);
		$criteria->compare('description_short',$this->description_short,true);
		$criteria->compare('description_long',$this->description_long,true);
        $criteria->compare('vint',$this->vint,true);
        $criteria->compare('product_information',$this->product_information,true);

        $criteria->compare( 'vintner.last_name', $this->nachname, true );
        $criteria->compare( 'vintner.first_name', $this->vorname, true );
//                $criteria->compare( 'vintner.first_name', $this->vintner_search, true );

        $criteria->with = array('vintner');

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
            'sort'=>array(
                'attributes'=>array(
                    'nachname'=>array(
                        'asc'=>'vintner.last_name',
                        'desc'=>'vintner.last_name DESC',
                    ),
                    'vorname'=>array(
                        'asc'=>'vintner.first_name',
                        'desc'=>'vintner.first_name DESC',
                    ),
                    '*',
                ),
            ),
            'pagination' => array(    // pagination property
            	'pageSize' => 25, // page size
			), 
		));
	}

    public function getId() {
        return 'Wine'.$this->wine_id;
    }

    public function getPrice() {
        return $this->sale_price_net;
    }
    
	protected function afterFind() {
	    parent::afterFind();

	    //$this->product_information = $this->grape_variety . " " . $this->location . " " . $this->quality . " " . $this->vintage; //. " " . $this->vintner->last_name;
	}
}
